home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / oath.lha / oath / test / timer.h < prev    next >
C/C++ Source or Header  |  1991-08-29  |  7KB  |  240 lines

  1. #ifndef TIMER_H
  2.  
  3. #define TIMER_H
  4.  
  5. //***************************************************************************
  6. //         TIMER :: A class for software performance analysis
  7. //***************************************************************************
  8. //
  9. //  Copyright (C) 1990, 1989  Texas Instruments Incorporated
  10. //  Permission is granted to any individual or institution
  11. //  to use, copy, modify, and distribute this software,
  12. //  provided that this complete copyright and permission notice
  13. //  is maintained, intact, in all copies and supporting documentation.
  14. //
  15. //  Texas Instruments Incorporated provides this software "as is"
  16. //  without express or implied warranty.
  17. //
  18. //***************************************************************************
  19. //
  20. //  History:
  21. //      06/90  Brian M Kennedy    New naming convention
  22. //      07/89  Brian M Kennedy    Rewritten in C++ (CADET)
  23. //      12/88  Brian M Kennedy    Added ability to have multiple timers (PPODEM)
  24. //             Joseph T Rahmeh    Original in C (UT Austin)
  25. //
  26. //***************************************************************************
  27. //
  28. //  This file provides timing code for performance evaluation of software.
  29. //  The timer class operates much like a stopwatch with a START/STOP button
  30. //  and a SPLIT button.  The start() operation resets the stopwatch to zero.
  31. //  The split() operation captures the amount of time since the last start().
  32. //  Thus, you can capture the time repeatedly by doing multiple split() cals
  33. //  after a single start() call.
  34. //
  35. //  Functions user(), system(), cpu(), and real() read the times captured by
  36. //  split(), where
  37. //
  38. //      User time:
  39. //          time CPU spends in user mode on behalf of the program.
  40. //    System time:
  41. //        time CPU spends in system mode on behalf of the program.
  42. //    All time:
  43. //        total CPU time.
  44. //    Real time:
  45. //        what you get from a stop watch timer (or the clock on the wall).
  46. //
  47. //  Functions which have the suffix "US" return time values in microseconds.
  48. //  All other functions return time values in milliseconds.
  49. //
  50. //  This code was adapted for C++ from C code written by Brian Kennedy
  51. //  for a project performed at UT Austin.  That code was derived from
  52. //  code originally written by Dr. Joseph Rahmeh at UT Austin.
  53. //
  54. //***************************************************************************
  55.  
  56. #include <sys/time.h>
  57.  
  58. #include <sys/resource.h>
  59.  
  60. extern int getrusage (int, rusage *);
  61.  
  62. #include <sys/types.h>
  63.  
  64. #include <sys/timeb.h>
  65.  
  66. extern int ftime (timeb *);
  67.  
  68. /***************************************************************************/
  69. // The TIMER Class Definition
  70.  
  71. class timer
  72.    {public:
  73.     inline timer();            // Constructs timer and does start()
  74.     inline void mark();              // Obsolete synonym for start()
  75.  
  76.     // Timer operation
  77.     inline void start();              // Reset timer to zero.
  78.     inline void split();        // Capture time since last start()
  79.  
  80.     // Read times from last start() to last split()
  81.     inline long real() const;    // real        time (ms)
  82.  
  83.     inline long user() const;    // user        time (ms)
  84.     inline long system() const;    // system      time (ms)
  85.     inline long cpu() const;    // user+system time (ms)
  86.  
  87.     inline long userUS() const;    // user        time (us)
  88.     inline long systemUS() const;    // system      time (us)
  89.     inline long cpuUS() const;    // user+system time (us)
  90.  
  91.     // Times for this process up to last split()
  92.     inline long totalUser() const;  // user        time (ms)
  93.     inline long totalSystem() const;// system      time (ms)
  94.     inline long totalCpu() const;   // user+system time (ms)
  95.  
  96.     private:
  97.     rusage Usage;       // current rusage structure
  98.         rusage Usage0;      // rusage structure at last mark
  99.         timeb  Real;        // current elapsed real time
  100.     timeb  Real0;       // elapsed real time at last mark
  101.  
  102.    }; // timer
  103.  
  104.  
  105. /***************************************************************************/
  106. //  Inline Member Functions
  107.  
  108.     inline void
  109. timer::mark()    // obsolete
  110.    {start();}
  111.  
  112.  
  113. // Manipulators
  114.  
  115.     inline void
  116. timer::start()
  117.    {getrusage(0, &Usage0);
  118.     ftime(&Real0);
  119.    }
  120.  
  121.     inline void
  122. timer::split()
  123.    {getrusage(0, &Usage);
  124.     ftime(&Real);
  125.    }
  126.  
  127.  
  128. // Constructor
  129.  
  130.     inline
  131. timer::timer()
  132.    {start();
  133.     split();
  134.    }
  135.  
  136.  
  137. // Accessors
  138.  
  139.     inline long
  140. timer::real() const
  141.    {long s  = Real.time    - Real0.time;
  142.     long ms = Real.millitm - Real0.millitm;
  143.     if(ms < 0)
  144.        {ms += 1000;
  145.     s--;
  146.        }
  147.     return 1000*s + ms;
  148.    }
  149.  
  150.     inline long
  151. timer::user() const
  152.    {long dsec  = Usage.ru_utime.tv_sec  - Usage0.ru_utime.tv_sec;
  153.     long dusec = Usage.ru_utime.tv_usec - Usage0.ru_utime.tv_usec;
  154.     if(dusec < 0)
  155.        {dusec += 1000000;
  156.     dsec--;
  157.        }
  158.     return(dsec*1000 + dusec/1000);
  159.    }
  160.  
  161.     inline long
  162. timer::system() const
  163.    {long dsec  = Usage.ru_stime.tv_sec  - Usage0.ru_stime.tv_sec;
  164.     long dusec = Usage.ru_stime.tv_usec - Usage0.ru_stime.tv_usec;
  165.     if(dusec < 0)
  166.        {dusec += 1000000;
  167.     dsec--;
  168.        }
  169.     return(dsec*1000 + dusec/1000);
  170.    }
  171.  
  172.     inline long
  173. timer::cpu() const
  174.    {long dsec  =   Usage.ru_utime.tv_sec   + Usage.ru_stime.tv_sec
  175.              - Usage0.ru_utime.tv_sec  - Usage0.ru_stime.tv_sec;
  176.     long dusec =   Usage.ru_utime.tv_usec  + Usage.ru_stime.tv_usec
  177.              - Usage0.ru_utime.tv_usec - Usage0.ru_stime.tv_usec;
  178.     if(dusec < 0)
  179.        {dusec += 1000000;
  180.     dsec--;
  181.        }
  182.     return(dsec*1000 + dusec/1000);
  183.    }
  184.  
  185.     inline long
  186. timer::userUS() const
  187.    {long dsec  = Usage.ru_utime.tv_sec  - Usage0.ru_utime.tv_sec;
  188.     long dusec = Usage.ru_utime.tv_usec - Usage0.ru_utime.tv_usec;
  189.     if(dusec < 0)
  190.        {dusec += 1000000;
  191.     dsec--;
  192.        }
  193.     return(dsec*1000000 + dusec);
  194.    }
  195.  
  196.     inline long
  197. timer::systemUS() const
  198.    {long dsec  = Usage.ru_stime.tv_sec  - Usage0.ru_stime.tv_sec;
  199.     long dusec = Usage.ru_stime.tv_usec - Usage0.ru_stime.tv_usec;
  200.     if(dusec < 0)
  201.        {dusec += 1000000;
  202.     dsec--;
  203.        }
  204.     return(dsec*1000000 + dusec);
  205.    }
  206.  
  207.     inline long
  208. timer::cpuUS() const
  209.    {long dsec  =   Usage.ru_utime.tv_sec   + Usage.ru_stime.tv_sec
  210.              - Usage0.ru_utime.tv_sec  - Usage0.ru_stime.tv_sec;
  211.     long dusec =   Usage.ru_utime.tv_usec  + Usage.ru_stime.tv_usec
  212.              - Usage0.ru_utime.tv_usec - Usage0.ru_stime.tv_usec;
  213.     if(dusec < 0)
  214.        {dusec += 1000000;
  215.     dsec--;
  216.        }
  217.     return(dsec*1000000 + dusec);
  218.    }
  219.  
  220.     inline long
  221. timer::totalUser() const
  222.    {return(  Usage.ru_utime.tv_sec  * 1000
  223.        + Usage.ru_utime.tv_usec / 1000);
  224.    }
  225.  
  226.     inline long
  227. timer::totalSystem() const
  228.    {return(  Usage.ru_stime.tv_sec  * 1000 
  229.        + Usage.ru_stime.tv_usec / 1000);
  230.    }
  231.  
  232.     inline long
  233. timer::totalCpu() const
  234.    {return(  (Usage.ru_utime.tv_sec  + Usage.ru_stime.tv_sec) *1000
  235.        + (Usage.ru_utime.tv_usec + Usage.ru_stime.tv_usec)/1000);
  236.    }
  237.  
  238. /***************************************************************************/
  239.  
  240. #endif // TIMER_H